package com.wangcang.widgetlibrary.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.bean.FilterBean;

import java.util.List;

/**
 *  筛选器的Adapter
 * Created by ShineF on 2017/7/11 0011.
 */

public class ScreenAdapter extends BaseAdapter {

    private int mType;
    private Context mContext;
    private List<FilterBean> mList;
    private ViewHolder mHolder;

    public ScreenAdapter(int type, Context context, List<FilterBean> list){
        this.mType = type;
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = View.inflate(mContext, R.layout.view_drawer_filter_list_layout, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        }else{
            mHolder = (ViewHolder) convertView.getTag();
        }
        final FilterBean filterBean = mList.get(position);
        if(filterBean.isSelect()){
            mHolder.mBtn.setSelected(true);
        }else{
            mHolder.mBtn.setSelected(false);
        }
        mHolder.mBtn.setText(filterBean.getText());
        mHolder.mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filterBean.setSelect(!filterBean.isSelect());
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    class ViewHolder{
        Button mBtn;
        public ViewHolder(View view){
                mBtn = (Button) view.findViewById(R.id.btn_item);
        }
    }
}
